import modelExtend from 'dva-model-extend'
import {lwjPageModel} from 'utils/model'
import pathToRegexp from 'path-to-regexp'
import {routerRedux} from 'dva/router'
import {query, editGrades, open, close, queryPrivileges, queryLevelPrivileges, updateLevelPrivilege, addLevelPrivilege, removeLevelPrivilege} from './service'
import lodash from 'lodash'
import { message } from 'antd'

const defaultList = [{
  minScore: 0,
  maxScore: null
},{
  minScore: null,
  maxScore: null
}]
export default modelExtend(lwjPageModel, {
  namespace: 'grades',
  state: {
    list: [],
    copyList: lodash.cloneDeep(defaultList),
    gradesModalVisible: false,
    privilegesModalVisible: false,
    item: {},
    enabled: false,
    privileges: [{'name': 1}]
  },
  subscriptions: {
    setup({dispatch, history}) {
      history.listen(({pathname, search}) => {
        const match = pathToRegexp('/member/grades').exec(pathname);
        if(match) {
          dispatch({
            type: 'query'
          })
          dispatch({
            type: 'queryPrivileges'
          })
        }
      })
    }
  },
  effects: {
    *query({isModal}, {call, put, select}) {
      let {result: data} = yield call(query, {});
      yield put({
        type: 'setGrades',
        list: data.levelItemList,
        enabled: data.enabled
      })
      yield put({
        type: 'setCopyGrades',
        copyList: (data.levelItemList && data.levelItemList.length > 0 ? lodash.cloneDeep(data.levelItemList) : lodash.cloneDeep(defaultList))
      })
      if(isModal){
        yield put({
          type: 'toggleGradesVisible',
          gradesModalVisible: true
        })
      }
    },
    *editGrades({list}, {call, put, select}) {
      let params = {
        levelItems: list
      }
      let {result: data} = yield call(editGrades, params);
      yield put({
        type: 'query'
      })
      yield put({
        type: 'toggleGradesVisible',
        gradesModalVisible: false
      })
    },
    *toggle({enabled}, {call, put, select}) {
      let _u = (enabled ? open : close);
      let {result: data} = yield call(_u);
      yield put({
        type: 'query'
      })
    },
    *queryPrivileges({}, {call, put, select}) {
      let {result: data} = yield call(queryPrivileges);
      yield put({
        type: 'setPrivileges',
        privileges: data
      })
    },
    *queryLevelPrivileges({item}, {call, put, select}) {
      let {result: data} = yield call(queryLevelPrivileges, {
        id: item.id
      });
      yield put({
        type: 'setPrivilegeItem',
        item: item
      })
      const privileges = yield select(({ grades }) => grades.privileges);
      data = data ? data : [];
      privileges.forEach((val1, index) => {
        privileges[index].items = JSON.parse(val1.tempData).items;
        privileges[index].targetId = null;
        data.forEach(val2 => {
          if(val1.id == val2.levelPrivilege.id ){
            privileges[index].items = JSON.parse(val2.data).items;
            privileges[index].targetId = val2.id;
          }
        })
      })
      console.log(privileges)
      yield put({
        type: 'setPrivileges',
        privileges: privileges
      })
    },
    *updateLevelPrivilege({params}, {call, put, select}) {
      let {result: data} = yield call(updateLevelPrivilege, params);
      message.success("更新成功");
      const item = yield select(({ grades }) => grades.item);
      yield put({
        type: 'queryLevelPrivileges',
        item: item
      })
    },
    *addLevelPrivilege({params}, {call, put, select}) {
      let {result: data} = yield call(addLevelPrivilege, params);
      message.success("启用成功");
      const item = yield select(({ grades }) => grades.item);
      yield put({
        type: 'queryLevelPrivileges',
        item: item
      })
    },
    *removeLevelPrivilege({params}, {call, put, select}) {
      let {result: data} = yield call(removeLevelPrivilege, params);
      message.success("已禁用");
      const item = yield select(({ grades }) => grades.item);
      yield put({
        type: 'queryLevelPrivileges',
        item: item
      })
    },
  },
  reducers: {
    setGrades(state, {list, enabled}) {
      return{
        ...state,
        list,
        enabled
      }
    },
    setCopyGrades(state, {copyList}) {
      return{
        ...state,
        copyList
      }
    },
    toggleGradesVisible(state, {gradesModalVisible}) {
      return{
        ...state,
        gradesModalVisible
      }
    },
    togglePrivilegesModalVisible(state, {privilegesModalVisible}) {
      return{
        ...state,
        privilegesModalVisible
      }
    },
    setPrivilegeItem(state, {item}) {
      return{
        ...state,
        item
      }
    },
    setPrivileges(state, {privileges}) {
      return{
        ...state,
        privileges
      }
    },
  }
})
